package xyz.fuqufyb.aec.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.fuqufyb.aec.data.JsonResult;
import xyz.fuqufyb.aec.entity.AdminUser;
import xyz.fuqufyb.aec.entity.Role;
import xyz.fuqufyb.aec.service.IAdminUserService;
import xyz.fuqufyb.aec.service.IRoleService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: AdminUserController
 * @date 时间: 2021/1/28 21:35
 */

@RestController
@RequestMapping("/sys")
public class AdminUserController {
    @Autowired
    private IAdminUserService userService;

    @Autowired
    private IRoleService roleService;

    /**
     * 分页数据
     *
     * @param maps
     * @return
     */
    @RequestMapping("/user/table")
    public JsonResult userPage(@RequestParam Map<String, Object> maps) {

        JsonResult jsonResult = userService.selectUserList(maps);

        if (jsonResult == null) {
            return JsonResult.getJsonError();
        }

        return jsonResult;
    }

    /**
     * 新增，修改
     *
     * @param adminUser
     * @param act
     * @return
     */
    @PostMapping("/user/html")
    public JsonResult updateAddUser(AdminUser adminUser, String act) {
        if (act.equals("add")) {
            int i = userService.insertSelective(adminUser);
            if (i > 0) {
                return JsonResult.getJsonSuccess("新增成功");
            }
        } else if (act.equals("update")) {

            int i = userService.updateByPrimaryKeySelective(adminUser);
            if (i > 0) {
                return JsonResult.getJsonError("修改成功");
            }
        }
        return JsonResult.getJsonError();
    }

    @RequestMapping("/user/del")
    public JsonResult deleteUser(@RequestParam("ids") Integer[] ids) {
        int i = userService.deleteByUserIds(Arrays.asList(ids));
        if (i > 0) {
            return JsonResult.getJsonSuccess("删除成功");
        }
        return JsonResult.getJsonError();
    }

    /**
     * 用户授权 数据
     *
     * @param userId
     * @return
     */
    @RequestMapping("/user/user_role")
    public JsonResult userRole(Integer userId) {

        List<Role> roles = roleService.getRoleListUserId(userId);

        return JsonResult.getJsonSuccess(roles);
    }

    /**
     * 所以角色 数据
     *
     * @return
     */
    @RequestMapping("/user/role_tree")
    public String roleTree() throws JsonProcessingException {
        List<Role> roleListUserId = roleService.getRoleList();

        if (roleListUserId != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(roleListUserId);

            return json;
        }

        return null;
    }

    /**
     *设置 权限
     * @param userId
     * @param roleIds
     * @return
     */
    @PostMapping("/user/assign_role")
    public JsonResult assignRole(Integer userId, @RequestParam("roleIds") Integer[] roleIds) {
        if (roleIds == null ||userId==0 || roleIds.length == 0){
            return JsonResult.getJsonError("权限不能为空");
        }
        Integer i = roleService.updateAssignRole(userId, Arrays.asList(roleIds));
        if (i > 0) {
            return JsonResult.getJsonSuccess("设置成功");
        }
        return JsonResult.getJsonError();
    }
}
